import pymysql
from sanic import Sanic
from sanic.response import html
from sanic.request import Request

from blueprints import AddBluePrints
from cookie import generateCookie, COOKIE_KEY
from my_logging.for_sanic_config import LOGGING_CONFIG

app = Sanic(__name__, log_config=LOGGING_CONFIG)
AddBluePrints(app)

app.static('/3rd', '../3rd/', name='3rd')
app.static('/3rd/layui', '../3rd/layui', name='layui')
app.static('/static', '../static/', name='static')


def open_stuff(path: str):
    with open(f'../static/html/{path}.html', 'r', encoding='utf-8') as f:
        html_str = f.read()

    return html(html_str)


@app.route('/create', methods=['GET'])
async def handle_get_create(request : Request):
    return open_stuff('create')


@app.route('/', methods=['GET'])
async def handleGetIndex(request : Request):
    response = open_stuff('index')

    cookie_in_request = request.cookies.get(COOKIE_KEY)
    if cookie_in_request is None:
        cookie = generateCookie()
        response.add_cookie(
            COOKIE_KEY, cookie,
            # domain='127.0.0.1',
            secure=False,
            httponly=True
        )

    return response


if __name__ == '__main__':
    app.run(debug = True, host = '0.0.0.0', port = 4399)
